<?php

namespace App\Http\Controllers\Back;


use App\Http\Controllers\Controller;
use App\Model\Partner;
use App\Model\UserCommissionDetail;
use Illuminate\Http\Request;
use DB;

/**
 * Class IncomeCountController
 * @package App\Http\Controllers\Back
 */
class IncomeCountController extends Controller
{
    private $user;

    public function __construct ()
    {
        $this->user = \Auth::user ();
    }


    public function index(Request $request)
    {
        $data = '';
        switch ($this->user->type) {
            case '0':
                $data = $this->admin($request);
                break;
            case '1':
                $data = $this->city($request);
                break;
            case '2':
                $data = $this->business($request);
                break;
            case '3':
                $data = $this->operation($request);
                break;
            case '4':
                echo 4;
                break;
            default:
                break;
        }
        return $this->renderJson($data);
    }

    /**
     * 业务员
     */
    public function operation($request)
    {

        $begin_month_time = date('Y-m-01 00:00:00', strtotime('-1 month'));
        $begin_time = date('Y-m-01 00:00:00');
        $end_time = date('Y-m-d 23:59:59');
        $day_time = date('Y-m-d 00:00:00');
        // 今日会员收益
        // 一级业务员
        $dayOneResult = DB::select('select sum(money) as money from `user_commission_details` where `relation` = ? and `relation_id` = ? and `created_at` between ? and ?', [3, $this->user->type, $day_time, $end_time]);
        $dayTwoResult = DB::select('select sum(money) as money from `user_commission_details` where `relation` = ? and `relation_id` = ? and `created_at` between ? and ?', [4, $this->user->type, $day_time, $end_time]);
        $dayThreeResult = DB::select('select sum(money) as money from `user_commission_details` where `relation` = ? and `relation_id` = ? and `created_at` between ? and ?', [5, $this->user->type, $day_time, $end_time]);

        $dayUserIncome = $dayOneResult[0]->money + $dayTwoResult[0]->money + $dayThreeResult[0]->money;

        //上月累计收益
        $beginMouthResult = DB::select('select sum(money) as money from `user_commission_details` where`relation_id` = ? and `created_at` between ? and ?', [$this->user->id, $begin_month_time, $begin_time]);

        //当月累计收益
        $beginResult = DB::select('select sum(money) as money from `user_commission_details` where`relation_id` = ? and `created_at` between ? and ?', [$this->user->id, $begin_time, $end_time]);

        //累计会员费收益
        $countUserMoney = DB::select('select sum(money) as money from `user_commission_details` where`relation_id` = ? ', [$this->user->id]);

        // 会员邀请员工收益+克隆收入

        $marketingMoney = DB::select("SELECT sum(amount) as money FROM user_bill WHERE user_id =?",[$this->user->id]);

        //会员邀请企业获取收益
        $enterpriseMoney = DB::select("SELECT sum(money) as money FROM enterprise_recharge_fenxiao_queue WHERE  relation=? and  relation_id=?",[$this->user->type, $this->user->id]);


        // 累计收益
        $countMoney = $marketingMoney[0]->money+$enterpriseMoney[0]->money+$countUserMoney[0]->money;

        return [
            'type' => $this->user->type,
            'countMoney' => $countMoney,
            'marketingMoney' => $marketingMoney[0]->money,
            'countUserMoney' => $countUserMoney[0]->money,
            'enterpriseMoney' => $enterpriseMoney[0]->money,
            'dayUserIncome' => $dayUserIncome,
            'lastMonthIncome' => $beginMouthResult[0]->money,
            'currentIncome' => $beginResult[0]->money
        ];
    }

    /**
     * 运营商
     * @return array
     */
    public function business()
    {

        $begin_month_time = date('Y-m-01 00:00:00', strtotime('-1 month'));
        $begin_time = date('Y-m-01 00:00:00');
        $end_time = date('Y-m-d 23:59:59');
        $day_time = date('Y-m-d 00:00:00');
        // 今日会员收益
        $dayUserIncome = DB::select('select sum(money) as money from `user_commission_details` where `relation` = ? and `relation_id` = ? and `created_at` between ? and ?', [6, $this->user->type, $day_time, $end_time]);

        //上月累计收益
        $beginMouthResult = DB::select('select sum(money) as money from `user_commission_details` where  `relation` = ? and `relation_id` = ? and `created_at` between ? and ?', [6, $this->user->type, $begin_month_time, $begin_time]);

        //当月累计收益
        $beginResult = DB::select('select sum(money) as money from `user_commission_details` where  `relation` = ? and `relation_id` = ? and `created_at` between ? and ?', [6, $this->user->type, $begin_time, $end_time]);

        //累计会员费收益
        $countUserMoney = DB::select('select sum(money) as money from `user_commission_details` where  `relation` = ? and `relation_id` = ? ', [6, $this->user->type]);

        //会员邀请企业获取收益
        $enterpriseMoney = DB::select("SELECT sum(money) as money FROM enterprise_recharge_fenxiao_queue WHERE  relation=? and  relation_id=?",[$this->user->type, $this->user->id]);

        return [
            'type' => $this->user->type,
            'countMoney' => $countUserMoney[0]->money, // 累计收益
            'countUserMoney' => $countUserMoney[0]->money,
            'enterpriseMoney' => $enterpriseMoney[0]->money,
            'dayUserIncome' => $dayUserIncome[0]->money,
            'lastMonthIncome' => $beginMouthResult[0]->money,
            'currentIncome' => $beginResult[0]->money
        ];
    }

    /**
     * 城市合伙人
     * @return array
     */
    public function city()
    {
        $p = Partner::where(['user_id' => $this->user->id])->first();
        $begin_month_time = date('Y-m-01 00:00:00', strtotime('-1 month'));
        $begin_time = date('Y-m-01 00:00:00');
        $end_time = date('Y-m-d 23:59:59');
        $day_time = date('Y-m-d 00:00:00');
        // 今日会员收益
        $dayUserIncome = DB::select('select sum(money) as money from `user_commission_details` where `relation` = ? and `relation_id` = ? and `created_at` between ? and ?', [7, $this->user->type, $day_time, $end_time]);

        //上月累计收益
        $beginMouthResult = DB::select('select sum(money) as money from `user_commission_details` where`relation` = ? and `relation_id` = ?  and `created_at` between ? and ?', [7, $this->user->type, $begin_month_time, $begin_time]);

        //当月累计收益
        $beginResult = DB::select('select sum(money) as money from `user_commission_details` where `relation` = ? and `relation_id` = ?  and `created_at` between ? and ?', [7, $this->user->id, $begin_time, $end_time]);
        // 今日代理收益
        $dayBusinessIncome = DB::select('select sum(agency_fee) as  agency_fee from `operators` where `examine` ="1" and `created_at` between ? and ? and `place_partner_id` = ?', [$day_time, $end_time, $p->id]);

        // 累计代理收益
        $countBusinessIncome = DB::select('select sum(agency_fee) as  agency_fee from `operators` where `examine` ="1" and `place_partner_id` = ?', [$p->id]);

        //累计会员费收益
        $countUserMoney = DB::select('select sum(money) as money from `user_commission_details` where`relation` = ? and `relation_id` = ?  ', [7, $this->user->id]);

        //会员邀请企业获取收益
        $enterpriseMoney = DB::select("SELECT sum(money) as money FROM enterprise_recharge_fenxiao_queue WHERE  relation=? and  relation_id=?",[$this->user->type, $this->user->id]);

        return [
            'type' => $this->user->type,
            'enterpriseMoney' => $enterpriseMoney[0]->money,
            'countMoney' => $countUserMoney[0]->money  + $countBusinessIncome[0]->agency_fee * 0.5, // 累计收益
            'countUserMoney' => $countUserMoney[0]->money,
            'dayUserIncome' => $dayUserIncome[0]->money,
            'lastMonthIncome' => $beginMouthResult[0]->money,
            'currentIncome' => $beginResult[0]->money,
            'dayBusinessIncome' => $dayBusinessIncome[0]->agency_fee * 0.5,
            'countBusinessIncome' => $countBusinessIncome[0]->agency_fee * 0.5
        ];
    }

    /**
     * 总平台
     * 总平台的代理 = 云营销所交代理费*50%
     */
    public function admin($request)
    {
        // 累计会员充值
        $countUserRecharge = DB::select('select sum(money) as money from `user_recharges` where `is_pay` = "1"');

        // 累计代理
        $countAgencyFee = DB::select('select sum(agency_fee) as  agency_fee from `operators` where `examine` ="1"');

        //今日会员充值
        $begin_time = date('Y-m-01 00:00:00');
        $day_time = date('Y-m-d 00:00:00');
        $end_time = date('Y-m-d 23:59:59');
        $dayUserRecharge = DB::select('select sum(money) as money from `user_recharges` where `is_pay` = "1" and `pay_time` between ? and ?', [$day_time, $end_time]);

        // 今日代理充值
        $dayAgencyFee = DB::select('select sum(agency_fee) as  agency_fee from `operators` where `examine` ="1" and `created_at` between ? and ?', [$day_time, $end_time]);

        // 上月会员累计充值
        $begin_month_time = date('Y-m-01 00:00:00', strtotime('-1 month'));
        $lastMouthUserRecharge = DB::select('select sum(money) as money from `user_recharges` where `is_pay` = "1" and `pay_time` between ? and ?', [$begin_month_time, $begin_time]);

        // 当月会员累计充值
        $end_time = date('Y-m-d 23:59:59');
        $currentMouthUserRecharge = DB::select('select sum(money) as money from `user_recharges` where `is_pay` = "1" and `pay_time` between ? and ?', [$begin_time, $end_time]);

        return [
            'type' => $this->user->type,
            'countUserRecharge' => $countUserRecharge[0]->money,
            'countAgencyFee' => $countAgencyFee[0]->agency_fee * 0.5,
            'dayUserRecharge' => $dayUserRecharge[0]->money,
            'dayAgencyFee' => $dayAgencyFee[0]->agency_fee * 0.5,
            'lastMouthUserRecharge' => $lastMouthUserRecharge[0]->money,
            'currentMouthUserRecharge' => $currentMouthUserRecharge[0]->money,
        ];
    }
}